//index.js
//获取应用实例
const app = getApp()
const http = require('../../utils/http');
const common = require('../../utils/common')
const timeUtil = require('../../utils/util.js');
Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        productInfo: {},
        icon: {
            share: '../images/icon/share.png',
            phone: '../images/icon/phone.png',
            more: '../images/icon/more.png',
            address: '../images/icon/address.png',
        }
    },
    //页面显示
    onShow: function () {

    },
    onLoad: function (options) {

        // 判断活动是否过期
        var time = timeUtil.formatTime(new Date());
        this.setData({
            time: time
        })


        //判断用户第三方授权没有
        common.getOpenid().then(openid => {
            this.isAuth(openid);
        });

        //进入页面传openid 缓存openid2
        this.setData({
            productId: options.productId
        });

        this.getProductDetail(options.productId);

    },

    isAuth: function (openid) {
        http._get('/mini/isAuth?miniOpenid=' + openid).then(res => {
            if (res.code == 200) {
                console.log("已经第三方授权");
            } else {
                wx.navigateTo({
                    url: '../publicToken/publicToken?openid=' + openid
                })
            }
        })
    },
    //获取商品详情
    getProductDetail: function (productId) {
        const body = {
            productId: productId,
        }
        http._objGet("/mini/getProductDetail", body).then(res => {
            if (res.code == 200) {

                //判断
                if (this.data.time >= res.result.endTime) {
                    this.setData({
                        'isOver': true,
                    });
                } else {
                    this.setData({
                        'isOver': false,
                    });
                }

                this.setData({
                    'product': res.result,
                    'fileList': res.result.fileList,
                    'merchant': res.result.merchantList,
                    'merchantPhone': res.result.merchantList[0].telephone,
                    'id': res.result.id
                });
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none',
                    duration: 1000,
                    mask: true
                })
                setTimeout(function () {
                    wx.hideToast()
                }, 2000)
            }

        }).catch(e => {
            console.log(e)
        });
    },
    getUserInfo: function (e) {
        console.log(e)
        app.globalData.userInfo = e.detail.userInfo
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    },
    //查看更多
    onChangeShowState: function () {
        let that = this;
        that.setData({
            showView: (!that.data.showView)
        })
    },
    //查看更多商家
    goMerchantList: function (e) {
        const productId = this.data.productId;
        console.log(productId);
        wx.navigateTo({
            url: '../merchantList/merchantList?productId=' + productId
        })
    },

    //拨打电话
    handleTel: function () {
        let that = this;
        wx.makePhoneCall({
            phoneNumber: that.data.merchantPhone
        })
    },
    //打开地图
    openMap: function () {

        // const latitude = 109.151545;
        // const longitude = 50.14561;
        //
        // wx.openLocation({
        //     latitude: Number(latitude),
        //     longitude: Number(longitude),
        //     name: '测试测试',
        //     scale: 28
        // })


        // wx.getLocation({
        //     type: 'gcj02',
        //     success: function (res) {
        //         console.log(res)
        //         wx.openLocation({
        //             latitude: res.latitude,
        //             longitude: res.longitude,
        //             name: '测试测试',
        //             scale: 28, // 缩放比例
        //         })
        //     }
        // })

    },
    // 购买优惠券
    receiveBuy: function () {
        common.getOpenid().then(openid => {
            const body = {
                "openid": openid,
                "productId": this.data.id
            }
            http._post('/mini/wxPay', body).then(res => {
                if (res.code == 200) {
                    wx.requestPayment({
                        timeStamp: res.result.timeStamp,
                        nonceStr: res.result.nonceStr,
                        package: res.result.packageValue,
                        signType: 'MD5',
                        paySign: res.result.paySign,
                        success(res) {
                            console.log(res)
                        },
                        fail(res) {
                            console.log(res)
                        }
                    })
                } else {
                    wx.showToast({
                        title: res.message,
                        icon: 'none',
                        duration: 3000,
                        mask: true
                    })
                }
            })

        });


    },
    //领取优惠券
    receiveTicket: function () {
        wx.showLoading({
            title: '领取中...',
        })
        common.getOpenid().then(openid => {
            let that = this;
            const body = {
                productId: that.data.id,
                openid: openid,
            }
            http._objGet("/mini/receiveProduct", body).then(res => {
                wx.hideLoading()
                if (res.code == 200) {
                    wx.showToast({
                        title: '领取成功',
                        duration: 1000,
                        mask: true
                    })
                } else {
                    wx.showToast({
                        title: res.message,
                        icon: 'none',
                        duration: 3000,
                        mask: true
                    })
                }

            })

        })


    },
    onShareAppMessage: function (e) {
        console.log(e);
        if (e.from === 'button') {

        }
        const productId = this.data.productId;
        const name = this.data.product.couponName;
        return {
            title: '优惠券',
            desc: name,
            path: '/pages/index/index?productId=' + productId
            // path: '/pages/product/product?productId=' + productId
        }
    },
})
